require 'rubygems'
gem 'msgpack'
require 'msgpack'
gem 'msgpack-rpc'
require 'msgpack/rpc'

ipport = nil
if ARGV.size < 7
  puts "Usage: SearchClient IPAddress:Port Query From Length And/or Fields <Field> Flag..."
  exit 0
else
  ipport = ARGV[0]
end

ipports = ipport.split(/:/)
exit 0 if ipports.size < 2

cl = MessagePack::RPC::Client.new(ipports[0], ipports[1].to_i)
cl.timeout = 5

argpack = cl.call(:argument)

andor = true if ARGV[4] == 'true'
andor = false if ARGV[4] == 'false'
f = ARGV[5].to_i
fields = ARGV[6..(6+f-1)]
flags = ARGV[(6+f), 1048576]

args = [ARGV[1], ARGV[2].to_i, ARGV[3].to_i, andor, f, *fields, *flags]

result = cl.call_apply(:search, *args)
